package com.intelligentmeterreadingsys.waterlist.querywaterlist.activity;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.json.JSONException;
import org.json.JSONObject;
import com.intelligentmeterreadingsys.R;
import com.intelligentmeterreadingsys.httpserver.RequestUrl;
import com.intelligentmeterreadingsys.httpserver.UploadHttpClient;
import com.intelligentmeterreadingsys.povo.WaterInfoVO;
import com.intelligentmeterreadingsys.util.SharedPreferenceUtil;
import com.intelligentmeterreadingsys.waterlist.copywater.db.CopyWaterDao;
import dbhelper.XmlNameHelper;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 抄表一键上传
 * @author king
 *
 */
public class OneKeyUploadActivity extends Activity{
    public Button button;//上传按钮
	 
	 public TextView textview1;//需上传总数
	 
	 public TextView textview2;//未上传总数
	 
	 public TextView textview3;//进度条描述
	 
	
	 public TextView textview4;
	 
	 public ProgressBar pb;//进度条
	 public List<WaterInfoVO> list=null;
	 public boolean isuploadfile=false;
	 
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.waterbanding_onetoupload);
		init();
		
	}
	
	/**
	 * 初始化控件、数据
	 */
	public void init(){
		
		   button=(Button)findViewById(R.id.waterbanding_upload_button);
		   pb=(ProgressBar)findViewById(R.id.waterbanding_progressBar);
		   textview1=(TextView)findViewById(R.id.waterbanding_upload_total);
		   textview2=(TextView)findViewById(R.id.waterbanding_noupload_total);
		   textview3=(TextView)findViewById(R.id.waterbanding_pb_textview);
		   textview4=(TextView)findViewById(R.id.onekeyupload_info);
		  
		   textview4.setText("抄表数据一键上传");
		   CopyWaterDao dao=new CopyWaterDao(this);
		   final int noupload=dao.NoUploadWater();
		   textview1.setText(dao.UploadWater()+"");
		   textview2.setText(noupload+"");
		   textview3.setText("0%");
		   
		   dao.close();
		   
		 
		   button.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(noupload<=0){
					Toast.makeText(OneKeyUploadActivity.this, "数据已经上传完了！", Toast.LENGTH_LONG).show();
					return;
				}
			
				CopyWaterDao dao=new CopyWaterDao(OneKeyUploadActivity.this);
				list=dao.queryAllNoUpload();
				
				dao.close();
				if(list==null|list.size()<1){
					
					return ;
				}
				pb.setMax(list.size());
				textview3.setText("正在上传中...");
				new Thread(){

					@Override
					public void run() {
						upload();
					}
					
				}.start();
				
			}
		});
	}
	
	/**
	 * 批量上传数据
	 * @return
	 */
	public boolean upload(){
		
		
		RequestUrl r=new RequestUrl(this);
		String url=r.getUrl(RequestUrl.COPYWATERLIST);
		
		SharedPreferenceUtil sp=new SharedPreferenceUtil(this);
		String imsi=sp.getValue(XmlNameHelper.IMSI);
		for(int i=0;i<list.size();i++){
			
			WaterInfoVO water=list.get(i);
			Map<String, String> map=new HashMap<String, String>();
			map.put("waternumber", water.getWaternumber());
			map.put("thismonthwater", water.getThismonthwater()+"");
			map.put("thisdate",water.getThisdate());
			map.put("usewater", water.getThismonthwater()-water.getLastmonthwater()+"");
			map.put("imsi",imsi);
			map.put("sid", "0");
			map.put("desc", water.getDesc());
			map.put("isnorfid", water.getIsNorfid());
			Map<File, String> mapfile=new HashMap<File, String>();
		    Map<String, String> maps=new HashMap<String, String>();

			List<String> lists=list.get(i).getImagepath();
			for(int k=0;k<lists.size();k++){
				File file=new File(lists.get(k));
				if(file.exists()){
					mapfile.put(file, "imagepath");
					maps.put(file.getName(), "imagename");
				}
				
			}
		
			
		String respon=	UploadHttpClient.uploadSubmit(url, maps, mapfile,map);
		
		if(respon.equals("error")){
			Message msg=myHandler.obtainMessage();
			msg.what=0;
		   myHandler.sendMessage(msg);
			return false;

		}
		try {
			JSONObject obj=new JSONObject(respon);
			
			String message=obj.getString("message");
			if(message.equals("yes")){
				Message msg=myHandler.obtainMessage();
				msg.what=1;
				msg.arg1=i+1;
				myHandler.sendMessage(msg);
				CopyWaterDao dao=new CopyWaterDao(this);
				dao.uploadstatus(water.getId());
				
				dao.close();
				
			}else{
				Message msg=myHandler.obtainMessage();
				msg.what=2;
			
				myHandler.sendMessage(msg);
				
				return false;
			}
		} catch (JSONException e) {
			
			Message msg=myHandler.obtainMessage();
			msg.what=2;
			
			myHandler.sendMessage(msg);
			return false;
		}
		
		}
		
		
		return true;
		
	}
        
	Handler myHandler=new Handler(){

		@Override
		public void handleMessage(Message msg) {
			if(msg.what==1){
				pb.setProgress(msg.arg1);
				
				textview3.setText("正在上传中...."+((float)msg.arg1)/(list.size())*100+"%");
			 
				if(msg.arg1==list.size()){
					
					textview3.setText("上传完毕100%");
					Toast.makeText(OneKeyUploadActivity.this, "上传成功", 1000).show();
					 CopyWaterDao dao=new CopyWaterDao(OneKeyUploadActivity.this);
					   final int noupload=dao.NoUploadWater();
					   textview1.setText(dao.UploadWater()+"");
					   textview2.setText(noupload+"");
					 
					   dao.close();
					
				}
			}else if(msg.what==2){
				Toast.makeText(OneKeyUploadActivity.this, "上传失败", 1000).show();
				textview3.setText("上传失败0%");
			}else if(msg.what==0){
				Toast.makeText(OneKeyUploadActivity.this, "网络连接错误", 1000).show();
				textview3.setText("网络连接错误");
			}
		}
		
	};
}
